<?php
	if($_POST){
		$ids = implode(',', _POST('chk'));
		foreach(_POST('chk') as $proid){
			if(file_exists('../media/product/images/100x100/slider-left-'.$proid.'.jpg')){
				unlink('../media/product/images/100x100/slider-left-'.$proid.'.jpg');
			}
			if(file_exists('../media/product/images/153x114/carousel-'.$proid.'.jpg')){
				unlink('../media/product/images/153x114/carousel-'.$proid.'.jpg');
			}
			if(file_exists('../media/product/images/162x179/product-'.$proid.'.jpg')){
				unlink('../media/product/images/162x179/product-'.$proid.'.jpg');
			}
			if(file_exists('../media/product/images/384x449/product-'.$proid.'.jpg')){
				unlink('../media/product/images/384x449/product-'.$proid.'.jpg');
			}
		}
		//PRODUCT COLOR
		$sql = "SELECT * FROM tblproduct_color WHERE product_id IN ($ids) ";
		$colors = $db->fetchAll($sql);
		foreach ( $colors as $color ) {
			if(file_exists('..'.$color['img_url'] )){
				unlink('..'.$color['img_url']);
			}
		}
		$db->delete('tblproduct_color', " product_id IN ($ids)");
		//PRODUCT IMAGE
		$sql = "SELECT * FROM tblproduct_img WHERE product_id IN ($ids)";
		$images = $db->fetchAll($sql);
		foreach ( $images as $image ) {
			if(file_exists('..'.$image['img_url'] )){
				unlink('..'.$image['img_url']);
			}
		}
		$db->delete('tblproduct_img', " product_id IN ($ids) ");
		$db->delete('tblproduct', " product_id IN ($ids) ");
		
	}else{
		$id = _POST('id');
		//PRODUCT COLOR
		$sql = "SELECT * FROM tblproduct_color WHERE product_id=$id";
		$colors = $db->fetchAll($sql);
		foreach ( $colors as $color ) {
			if(file_exists('..'.$color['img_url'] )){
				unlink('..'.$color['img_url']);
			}
		}
		if(file_exists('../media/product/images/100x100/slider-left-'.$id.'.jpg')){
			unlink('../media/product/images/100x100/slider-left-'.$id.'.jpg');
		}
		if(file_exists('../media/product/images/153x114/carousel-'.$id.'.jpg')){
			unlink('../media/product/images/153x114/carousel-'.$id.'.jpg');
		}
		if(file_exists('../media/product/images/162x179/product-'.$id.'.jpg')){
			unlink('../media/product/images/162x179/product-'.$id.'.jpg');
		}
		if(file_exists('../media/product/images/384x449/product-'.$id.'.jpg')){
			unlink('../media/product/images/384x449/product-'.$id.'.jpg');
		}
		$db->delete('tblproduct_color', " product_id=$id ");
		//PRODUCT IMAGE
		$sql = "SELECT * FROM tblproduct_img WHERE product_id=$id";
		$images = $db->fetchAll($sql);
		foreach ( $images as $image ) {
			if(file_exists('..'.$image['img_url'] )){
				unlink('..'.$image['img_url']);
			}
		}
		$db->delete('tblproduct_img', " product_id=$id ");
		$db->delete('tblproduct', " product_id=$id ");
	}
	redir('?c=product&a=list');